package com.teziyou.commodity.web.jfinal;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.teziyou.commodity.constant.Global;
import com.teziyou.commodity.enumtype.EnableEnum;
import com.teziyou.commodity.model.Area;
import com.teziyou.commodity.model.Distributor;
import com.teziyou.commodity.util.Lang;
import com.teziyou.commodity.validator.DistributorValidator;

@SuppressWarnings({"rawtypes", "unchecked"})
public class DistributorAction extends BaseController {
	
	public void list() {
		String select = "select * ";
		Page<Distributor> page = Distributor.dao.paginate(1, Global.pageSize, select, " from distributor");
		
		for(Distributor d:page.getList()) {
			String countyId = d.getInt("adm_div_id")+"";
			int provinceId = Lang.toInt(countyId.substring(0, 6));
			int cityId = Lang.toInt(countyId.substring(0, 8));
			Area province = Area.dao.findById(provinceId);
			Area city = Area.dao.findById(cityId);
			d.put("province", province);
			d.put("city", city);
			int isok = d.getInt("isOK");
			d.put("isok_display", EnableEnum.getEnumByCode(isok).getDesc());
			if(isok==EnableEnum.ON.getCode())
				d.put("toggle_desc", EnableEnum.OFF.getDesc());
			else if(isok==EnableEnum.OFF.getCode())
				d.put("toggle_desc", EnableEnum.ON.getDesc());
		}
		
		setAttr("page", page);
		setAttr("provinces", Area.dao.getAreasByPid(1001));
		
		renderJsp("list.jsp");
	}
	
	public void table() {
		String name = getPara("name");
		String admDivId = getPara("admDivId");
		int pageNo = getParaToInt("pageNo", 1);
		String select = "select * ";
		String sql = " from distributor where 1=1 ";
		List params = new ArrayList();
		if(StringUtils.isNotBlank(name)) {
			sql += "and name like ? ";
			params.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(admDivId)) {
			sql += "and adm_div_id like ? ";
			params.add(admDivId+"%");
		}
		Page<Distributor> page = Distributor.dao.paginate(pageNo, Global.pageSize, select, sql, params.toArray());
		
		for(Distributor d:page.getList()) {
			String countyId = d.getInt("adm_div_id")+"";
			int provinceId = Lang.toInt(countyId.substring(0, 6));
			int cityId = Lang.toInt(countyId.substring(0, 8));
			Area province = Area.dao.findById(provinceId);
			Area city = Area.dao.findById(cityId);
			d.put("province", province);
			d.put("city", city);
			int isok = d.getInt("isOK");
			d.put("isok_display", EnableEnum.getEnumByCode(isok).getDesc());
			if(isok==EnableEnum.ON.getCode())
				d.put("toggle_desc", EnableEnum.OFF.getDesc());
			else if(isok==EnableEnum.OFF.getCode())
				d.put("toggle_desc", EnableEnum.ON.getDesc());
		}
		
		setAttr("page", page);
		renderJsp("table.jsp");
	}
	
	public void delete() {
		String idStr = getPara("distributorIds");
		if(StringUtils.isNotBlank(idStr)) {
			String[] ids = idStr.split(",");
			for(String id:ids) {
				Distributor.dao.deleteById(id);
			}
		}
		renderJson(success("删除成功！！"));
	}
	
	@Before(DistributorValidator.class)
	public void save() {
		Distributor distributor = getModel(Distributor.class);
		distributor.save();
		renderJson(success("保存成功！！"));
	}
	
	public void updateView() {
		Distributor d = Distributor.dao.findById(getPara());
		setAttr("distributor", d);
		String countyId = d.getInt("adm_div_id")+"";
		int provinceId = Lang.toInt(countyId.substring(0, 6));
		int cityId = Lang.toInt(countyId.substring(0, 8));
		
		setAttr("provinceId", provinceId);
		setAttr("cityId", cityId);
		setAttr("countyId", countyId);
		
		setAttr("provinces", Area.dao.getSameLevelAreas(provinceId));
		setAttr("citys", Area.dao.getSameLevelAreas(cityId));
		setAttr("countys", Area.dao.getSameLevelAreas(Integer.parseInt(countyId)));
		renderJsp("update.jsp");
	}
	
	@Before(DistributorValidator.class)
	public void update() {
		Distributor distributor = getModel(Distributor.class);
		distributor.update();
		renderJson(success("保存成功！！"));
	}
	
	public void toggle() {
		Distributor d = Distributor.dao.findById(getPara());
		int isok = d.getInt("isOK");
		String desc = EnableEnum.ON.getDesc();
		if(isok==EnableEnum.OFF.getCode()) {
			d.set("isOK", EnableEnum.ON.getCode());
			desc = EnableEnum.OFF.getDesc();
		} else if(isok==EnableEnum.ON.getCode()) {
			d.set("isOK", EnableEnum.OFF.getCode());
		}
		d.update();
		renderJson(success(desc+"成功！！"));
	}

}
